The sample()
operator on Flow
sets up a timer to go off periodically,
based on the number of milliseconds that you supply as a parameter (here, 250 milliseconds).
When the timer goes off, whatever the last object was that had been emitted
upstream is passed along downstream, if that object had not been previously passed
along. This continues until the flow is closed or canceled.
So, in this case, 0 and 1 get emitted by the source Flow
during the first 250-millisecond sampling
window, so 1 gets emitted downstream. 2 and 3 get emitted by the source Flow
during the second 250-millisecond sampling window, so 3 gets emitted downstream.
And the source Flow
is closed before the third 250-millisecond sampling window
ends, so the Flow
returned by sample()
is closed, without emitting 4.